<div class="tableform">
  <h3>仓库添加/编辑</h3>
  <div class="division">
    <form method="post" action="index.php?app=ome&ctl=admin_branch&act=addbranch" id="form-branch">
      <table width="100%" cellspacing="0" cellpadding="0" border="0" >
        <tbody>
          <tr>
            <th><em class="c-red">*</em> 仓库名称：</th>
            <td><input type="text" name="name"  value="<{$branch.name}>" vtype="required"/>
              <{help}><{t}>仓库在系统中的中文名称标识<{/t}><{/help}> </td>
          </tr>
          <tr>
            <th><em class="c-red">*</em> 仓库编号：</th>
            <td><input type="text" name="branch_bn"  value="<{$branch.branch_bn}>" vtype="required"/>
              <{help}><{t}>仓库在系统中的编号<{/t}><{/help}> </td>
          </tr>
          <tr>
            <th>联系人姓名：</th>
            <td><input type="text" size="12" name="uname" value="<{$branch.uname}>"/>
              <{help}><{t}>仓库负责人的姓名<{/t}><{/help}> </td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <th>性别：</th>
            <td><input name="sex" type="radio" value="male" <{if $branch.sex=='male'}>checked="checked" <{/if}>/>
              男 <input type="radio" name="sex" value="female" <{if $branch.sex=='female'}>checked="checked" <{/if}>/>
              女</td>
            <td width="49%"></td>
          </tr>
          <tr>
            <th>仓库辐射区域：</th>
            <td><div id="deliveryAreaToggle">
                <label><input type="radio" onclick="set_vtype(0);" value="1" name="setting" <{if !$area}>checked="checked"<{/if}> >全国</label>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <label> <input type="radio" onclick="set_vtype(1);" value="0" name="setting" <{if $area}>checked="checked"<{/if}> >指定地区</label>
                <{help}><{t}>仓库负责配送包裹的城市管理<{/t}><{/help}> </div></td>
            <th>&nbsp;</th>
            <td>&nbsp;</td>
          </tr>
          <tr id='def_dexp'> </tr>
          <tr id="deliveryAreabox" style="display: none;">
            <th></th>
            <td ><table width="100%" border="0" cellpadding="0" cellspacing="0">
                <tr>
                  <td><div  id="deliveryArea-<{$branch.branch_id}>" class='deliveryArea'>
                      <ol style='list-style-type:decimal; margin:0; padding:0;list-style:decimal'>
                        <li class='division' style="line-height:30px;">
                          <div class='deliverycity'>
                            <input style="width:350px;" type='text' name='area_conf[areaGroupName]' readonly=true required="true" value='<{$areas_name}>' onclick="regionSelect(this);">
                            <input type='hidden' name='area_conf[areaGroupId]' value="<{$area}>" vtype=""  id="branch_area" class="_x_ipt" caution="仓库地区不能为空"/>
                            <{img class='regionSelect' app="desktop" src='bundle/editcate.gif' style='cursor:pointer;' onclick='regionSelect(this)' alt='地区选择' title="地区选择"}> </div>
                        </li>
                      </ol>
                    </div></td>
                </tr>
              </table></td>
          </tr>
          <tr>
            <th>地址：</th>
            <td colspan="2"><input type="text" name="address" value="<{$branch.address}>"/>
              <{help}><{t}>仓库所在城市的详细地址<{/t}><{/help}> </td>
          </tr>
          <tr>
            <th>邮编：</th>
            <td colspan="2"><input type="text" size="6" name="zip" value="<{$branch.zip}>"/></td>
          </tr>
          <tr>
            <th>电话：</th>
            <td colspan="2"><input type="text" size="15" name="phone" value="<{$branch.phone}>"/>
              <{help}><{t}>仓库的固定电话<{/t}><{/help}> </td>
          </tr>
          <tr>
            <th>手机：</th>
            <td colspan="2"><input type="text" size="12" name="mobile" value="<{$branch.mobile}>"/>
              <{help}><{t}>仓库负责人的移动联系方式<{/t}><{/help}> </td>
          </tr>
          <tr>
            <th>阀值：</th>
            <td colspan="2"><input type="text" size="6" vtype="number" name="stock_threshold" value="<{$branch.stock_threshold}>"/>
              <{help}><{t}>订单确认界面用来标识当前操作库存状态：无货、紧张、有货<{/t}><{/help}> </td>
          </tr>
          <tr>
            <th>仓库属性：</th>
            <td colspan="2">
             <input name="attr" type="radio" value="true" <{if $branch.attr=='true' || $branch.attr==''}>checked="checked" <{/if}>/>线上 
             <input type="radio" name="attr" value="false" <{if $branch.attr=='false'}>checked="checked" <{/if}>/>线下
             <{help}><{t}>用于设置是否需要回写前端店铺库存<{/t}><{/help}> </td>
          </tr>
          <tr>
            <th>备注：</th>
            <td colspan="2"><textarea name="memo" rows="6" cols="80"><{$branch.memo}></textarea>
              <{help}><{t}>对仓库附加的其它说明<{/t}><{/help}> </td>
          </tr>
          <tr>
              <th>权重：</th>
              <td colspan="2"><{input type="number&&required" value=$branch.weight|default:0 name="weight" size=2}></td>
          </tr>
        </tbody>
      </table>
      <div class="table-action"><{button label="提交" type="submit" name="submit" id="btn-branch"}> &nbsp;&nbsp; <{button label="关闭" onclick="javascript:void(window.close());" class="btn-secondary" type="button"}></div>
      <{if $branch.branch_id}>
      <input type="hidden" name="branch_id" value="<{$branch.branch_id}>">
      <{/if}>
    </form>
  </div>
</div>
<script>
function set_vtype(type){
	if (type == '1'){
	    $('branch_area').set('vtype', 'required');    
	}else{
		$('branch_area').set('vtype', '');    
	}
		
}

$('form-branch').store('target',{
    onRequest:function(){
       $('btn-branch').set('disabled', 'true');
    },
    onComplete:function(jsontext){
       var json = Json.evaluate(jsontext);
       if (typeof(json.error) != 'undefined'){
           $('btn-branch').set('disabled', '');
       }else{
           $('btn-branch').set('disabled', 'true');
           opener.finderGroup['<{$env.get.finder_id}>'].refresh.delay(400,opener.finderGroup['<{$env.get.finder_id}>']);
           window.close();
       }
    }
});

function deliveryAreaToggle(radio) {
	var tradio = radio;
	var tradioValue = tradio.get('value');
	var tmap = {
		'1': $('def_dexp'),
		'0': $('deliveryAreabox')
	};
	if (tradio.checked) {
		tmap[tradioValue].show();
		tmap[tradioValue].getFormElements().set('disabled', false);
	} else {
		tmap[tradioValue].hide();

		tmap[tradioValue].getFormElements().set('disabled', true);
	}
}

$$("#deliveryAreaToggle input[name=setting]").addEvent('click', function() {
	$$("#deliveryAreaToggle input[name=setting]").each(deliveryAreaToggle);
}).each(deliveryAreaToggle);

function chaexps(el) {
	var dexps = $ES('.deliveryexp', $(el).getParent('.deliveryexpbox'));
	dexps.toggleDisplay();
	dexps.each(function(i) {
		if (i.getElement('input[name^=useexp]')) {
			if (!i.isDisplay()) {
				i.getElement('input[name^=useexp]').set('value', 0);
			} else {
				i.getElement('input[name^=useexp]').set('value', 1);
			}
		}
		else if (i.getElement('input[name^=dt_useexp]')) {
			if (!i.isDisplay()) {
				i.getElement('input[name^=dt_useexp]').set('value', 0);
			} else {
				i.getElement('input[name^=dt_useexp]').set('value', 1);
			}
		}
	});
}

void function() {
	//var btn_add = $E('#deliveryArea-<{$branch.branch_id}> .add-dlyarea');
	var getItemTemplete = function() {
		var btn = '<{button label="验证" class="checkexp" onclick="checkExp(this);"}>';
		var itemTemplete = "<div class='deliverycity'>";
		itemTemplete += "<span style='float:right;'";
		itemTemplete += " onclick='if(!confirm(\"删除后无法恢复,确定要删除吗?\"))return;$(this).getParent(\"li\").remove()' ";
		itemTemplete += '><{img src="images/bundle/delecate.gif" style="cursor:pointer;" alt="删除" title="删除"}></span>仓库地区 ';
		itemTemplete += "<input style='width:300px;' type='text' name='areaGroupName[]' readonly=true  value='' class='_x_ipt' vvtype='required' caution='仓库地区不能为空' onclick=\"regionSelect(this);\">";
		itemTemplete += "<input type='hidden' name='areaGroupId[]' value=''/>";
		itemTemplete += '<{img class="regionSelect" src="images/bundle/editcate.gif" style="cursor:pointer;" onclick="regionSelect(this)" alt="编辑地区" title="编辑地区"}>';
		//itemTemplete+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label><input type='checkbox' name='hasCod[]' value='1'/><{t}>支持货到付款<{/t}></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		itemTemplete += "</div>";
		itemTemplete += "<div class='deliveryexpbox'><div class='deliveryexp'>";
		itemTemplete += "首重费用 <input style='width:30px;' type='text' name='firstFee[]' class='_x_ipt' vvtype='required&&number' caution='该项必填且只允许填写数字金额' value='' >";
		itemTemplete += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;续重费用 <input style='width:30px;' type='text' name='continueFee[]' class='_x_ipt' vvtype='required&&number' caution='该项必填且只允许填写数字金额' value='' >";
		itemTemplete += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class='lnk chgexp' onclick='chaexps(this)'><{t}>使用公式<{/t}></span>";
		itemTemplete += "</div>";
		itemTemplete += "<div class='deliveryexp' style='display:none'>";
		itemTemplete += "仓库公式 <input style='width:300px;' type='text' name='expressions[]' value='' vvtype='required&&checkExp1&&checkExp2' />";
		itemTemplete += btn + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class='lnk chgexp' onclick='chaexps(this)'><{t}>取消公式<{/t}></span>";
		itemTemplete += "<input type='hidden' name='useexp[]' value='0'/>";
		itemTemplete += "</div></div>";

		return itemTemplete;
	};
	var list = $E('#deliveryArea-<{$branch.branch_id}> ol');
	/*btn_add.addEvent('click', function() {
		var newitem = new Element('li', {
			'class': 'division',
			'style': 'line-height:30px;'
		}).set('html', getItemTemplete()).inject(list);
		regionSelect(newitem.getElement('.regionSelect'));
	});*/

	//$$('#dltype-info-<{$branch.branch_id}> .chgexp').each(chaexps);

} ();
function regionSelect(el) {
	var el = $(el).getParent('.deliverycity');
	var iptText = el.getElement('input[type=text]');
	var iptHidden = el.getElement('input[type=hidden]');
	Ex_Loader('modedialog',function(){
	    new ModeDialog('index.php?app=ectools&ctl=regions&act=showRegionTreeList&p[0]=' + el.uid + '&p[1]=multi', {
		width: 270,
		height: 340,
		params: {
			iptText: iptText,
			iptHidden: iptHidden
		}
	    });
	});
}
var checkExp = function(btn) {
	btn = $(btn);
	var ipt = btn.getPrevious('input');
	var expValue = ipt.getValue();
	new Dialog('index.php?app=ome&ctl=admin_dly_corp&act=checkExp&expvalue=' + encodeURIComponent(expValue), {
		modal: true,
		title: '验算仓库公式'
	});
}
</script>
